package com.itbaizhan.myorder.controller;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController  //返回json格式的数据
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /*订单是否支付，用订单id，在数据库中查看是否支付,所以只传id就可以*/
    //下单  在前端页面点击控制器地址  在前端页面的URL把oderId传进来
    @GetMapping("/place/{orderId}")
    //@PathVariable  URL中的 {xxx} 占位符可以通过 @PathVariable(“xxx”) 绑定到操作方法的入参中。
    public String placeOrder(@PathVariable String orderId){
        System.out.println("处理订单数据。。。。。");
        rabbitTemplate.convertAndSend("order_exchange","order_routing",orderId);
        return "下单成功，修改库存";
    }

    //用插件的延迟队列(推荐使用)
    //下单
    @GetMapping("/place2/{orderId}")
    public String placeOrders(@PathVariable String orderId) {
        System.out.println("处理订单数据。。。。。");
        //设置消息的延迟时间为10s
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(10000);
                return message;
            }
        };
        //订单发送到队列
        rabbitTemplate.convertAndSend("delayed_exchange","order_routing",orderId,messagePostProcessor);
        return "下单成功，修改库存";
    }
}


/*创建延迟交换机和延迟队列

@Configuration
public class RabbitConfig2 {
  public final String DELAYED_EXCHANGE = "delayed_exchange";
  public final String DELAYED_QUEUE = "delayed_queue";

  //1.延迟交换机
  @Bean(DELAYED_EXCHANGE)
  public Exchange delayedExchange() {
    // 创建自定义交换机
    Map<String, Object> args = new HashMap<>();
    args.put("x-delayed-type", "topic"); // topic类型的延迟交换机
    return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
   }

  //2.延迟队列
  @Bean(DELAYED_QUEUE)
  public Queue delayedQueue() {
    return QueueBuilder
         .durable(DELAYED_QUEUE)
         .build();
   }

  // 3.绑定
  @Bean
  public Binding bindingDelayedQueue(@Qualifier(DELAYED_QUEUE) Queue queue, @Qualifier(DELAYED_EXCHANGE) Exchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with("order_routing").noargs();
   }
}
编写下单的控制器方法

@GetMapping("/place2/{orderId}")
public String placeOrder2(@PathVariable String orderId) {
  System.out.println("处理订单数据...");

  // 设置消息延迟时间为10秒
  MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
      message.getMessageProperties().setDelay(10000);
      return message;
     }
   };
  // 将订单id发送到订单队列
  rabbitTemplate.convertAndSend("delayed_exchange", "order_routing", orderId, messagePostProcessor);
  return "下单成功，修改库存";
}
编写延迟队列的消费者

@RabbitListener(queues = "delayed_queue")
public void listenMessage(String orderId){
  System.out.println("查询"+orderId+"号订单的状态，如果已支付则无需处理，如果未支付则需要回退库存");
}
下单测试：访问http://localhost:8080/place2/10002

http://192.168.66.101:15672/ 的队列中不会显示 ，等待完，直接被消费者签收了*/